package protocol

import (
	"fmt"
)

type ProtocolFactoryMgr struct {
	factory map[string]Factory
}

var mgr = ProtocolFactoryMgr{
	factory: make(map[string]Factory),
}

func (pMgr *ProtocolFactoryMgr) regProtocolFactory(name string, p Factory) {
	pMgr.factory[name] = p
}
func RegisterProtocolFactory(name string, p Factory) {
	mgr.regProtocolFactory(name, p)
}
func IsRegisterProtocolFactory(name string) (isRegister bool, err error) {
	_, ok := mgr.factory[name]
	if !ok {
		err = fmt.Errorf("Not found %s Protocol", name)
		isRegister = false
		return
	}
	isRegister = true
	return
}
func GetProtocolInst(name string) (proInst Protocol, err error) {
	inst, ok := mgr.factory[name]
	if !ok {
		err = fmt.Errorf("Not found %s ProtocolFactory", name)
		return
	}
	proInst = inst.Create()
	return
}
